﻿using System;
using System.Collections;
using System.Data.SqlTypes;
using System.IO;
using Microsoft.SqlServer.Server;
using sqlserverextensions.Helper;

    public partial class UserDefinedFunctions
    {
        [Microsoft.SqlServer.Server.SqlFunction(
           DataAccess = DataAccessKind.None,
           FillRowMethodName = "FillRowList",
           IsDeterministic = true,
           Name = "Dir",
           TableDefinition = @"   Name NVARCHAR(4000)
                            , IsDirectory BIT
                            , Size BIGINT
                            , DateCreated DATETIME
                            , DateModified DATETIME
                            , FileExtension NVARCHAR(4000)
                            , IsReadOnly BIT
                            , DateLastAccess DATETIME
                            , DateLastWrite DATETIME
                            "
           )]
        public static IEnumerable Dir(SqlString path, SqlString filter)
        {
            Int16 errSeverity = 16;

            try
            {
                DirectoryInfo dirInfo = new DirectoryInfo(path.Value);
                if (filter.IsNull || filter.Value == string.Empty)
                    return dirInfo.GetFileSystemInfos();
                else
                    return dirInfo.GetFileSystemInfos(filter.Value);
            }
            catch (Exception ex)
            {
                ExceptionHelper.EvaluateException(ex, errSeverity);
                return null;
            }
        }

        private static void FillRowList(object obj, out SqlString altname, out SqlBoolean directoryflag, out SqlInt64 size, out SqlDateTime datetimecreate, out SqlDateTime datetimemodified, out SqlString extension, out SqlBoolean isReadOnly, out SqlDateTime datetimelastaccess, out SqlDateTime datetimelastwrite)
        {
            try
            {
                if (obj is FileInfo)
                {
                    FileInfo fsi = (FileInfo)obj;
                    altname = fsi.Name;
                    directoryflag = SqlBoolean.False;
                    size = fsi.Length;
                    datetimecreate = fsi.CreationTime;
                    datetimemodified = fsi.LastWriteTime;
                    extension = fsi.Extension.ToString();
                    isReadOnly = fsi.IsReadOnly;
                    datetimelastaccess = fsi.LastAccessTime;
                    datetimelastwrite = fsi.LastWriteTime;
                }
                else
                {
                    FileSystemInfo fsi = (FileSystemInfo)obj;
                    altname = fsi.Name;
                    directoryflag = SqlBoolean.True;
                    size = SqlInt64.Null;
                    datetimecreate = fsi.CreationTime;
                    datetimemodified = fsi.LastWriteTime;
                    extension = fsi.Extension.ToString();
                    isReadOnly = SqlBoolean.Null;
                    datetimelastaccess = fsi.LastAccessTime;
                    datetimelastwrite = fsi.LastWriteTime;
                }
            }
            catch (Exception e)
            {
                throw e;
            }
        }
    }

